home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DJGPP / BNU22SR1.ZIP / src / binutils.2 / bfd / reloc16.c < prev    next >
C/C++ Source or Header  |  1993-05-30  |  10KB  |  418 lines

  1. /* 8 and 16 bit COFF relocation functions, for BFD.
  2.    Copyright 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
  3.    Written by Cygnus Support.
  4.  
  5. This file is part of BFD, the Binary File Descriptor library.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. /* 
  22. Most of this hacked by  Steve Chamberlain,
  23.             sac@cygnus.com 
  24. */
  25.  
  26. /* These routines are used by coff-h8300 and coff-z8k to do
  27.    relocation.  */
  28.  
  29. #include "bfd.h"
  30. #include "sysdep.h"
  31. #include "libbfd.h"
  32. #include "seclet.h"
  33. #include "obstack.h"
  34. #include "coff/internal.h"
  35. #include "libcoff.h"
  36.  
  37. extern bfd_error_vector_type bfd_error_vector;
  38.  
  39. bfd_vma 
  40. DEFUN(bfd_coff_reloc16_get_value,(reloc, seclet),
  41.       arelent  *reloc AND
  42.       bfd_seclet_type *seclet)
  43. {
  44.   bfd_vma value;
  45.   asymbol *symbol = *(reloc->sym_ptr_ptr);
  46.   /* A symbol holds a pointer to a section, and an offset from the
  47.      base of the section.  To relocate, we find where the section will
  48.      live in the output and add that in */
  49.  
  50.   if (symbol->section == &bfd_und_section)
  51.   {
  52.     /* Ouch, this is an undefined symbol.. */
  53.     bfd_error_vector.undefined_symbol(reloc, seclet);
  54.     value = symbol->value;
  55.   }
  56.   else 
  57.   {
  58.     value = symbol->value +
  59.      symbol->section->output_offset +
  60.       symbol->section->output_section->vma;
  61.   }
  62.   
  63.   
  64.   /* Add the value contained in the relocation */
  65.   value += reloc->addend;
  66.   
  67.   return value;
  68. }
  69.  
  70. static void
  71. DEFUN(perform_slip,(s, slip, input_section, value),
  72.       asymbol **s AND
  73.       unsigned int slip AND
  74.       asection *input_section AND
  75.       bfd_vma value)
  76. {
  77.   
  78.   /* Find all symbols past this point, and make them know
  79.      what's happened */
  80.   while (*s) 
  81.   {
  82.     asymbol *p = *s;
  83.     if (p->section == input_section) 
  84.     {
  85.       /* This was pointing into this section, so mangle it */
  86.       if (p->value > value)
  87.       {
  88.     p->value -= slip;
  89.       }
  90.     }
  91.     s++;
  92.     
  93.   }    
  94. }
  95. static int 
  96. DEFUN(movb1,(input_section, symbols, r, shrink),
  97.       asection *input_section AND
  98.       asymbol **symbols AND
  99.       arelent *r AND
  100.       unsigned int shrink) 
  101. {
  102.   bfd_vma value = bfd_coff_reloc16_get_value(r,0);
  103.     
  104.   if (value >= 0xff00)
  105.   { 
  106.  
  107.     /* Change the reloc type from 16bit, possible 8 to 8bit
  108.        possible 16 */
  109.     r->howto = r->howto + 1;      
  110.     /* The place to relc moves back by one */
  111.     r->address -=1;
  112.       
  113.     /* This will be two bytes smaller in the long run */
  114.     shrink +=2 ;
  115.     perform_slip(symbols, 2, input_section, r->address - shrink +1);
  116.  
  117.       
  118.   }      
  119.   return shrink;      
  120. }
  121.  
  122. static int 
  123. DEFUN(jmp1,(input_section, symbols, r, shrink),
  124.       asection *input_section AND
  125.       asymbol **symbols AND
  126.       arelent *r AND
  127.       unsigned int shrink) 
  128. {
  129.  
  130.   
  131.   bfd_vma value = bfd_coff_reloc16_get_value(r, 0);
  132.     
  133.   bfd_vma dot = input_section->output_section->vma +
  134.    input_section->output_offset + r->address;    
  135.   bfd_vma gap;
  136.   
  137.   /* See if the address we're looking at within 127 bytes of where
  138.      we are, if so then we can use a small branch rather than the
  139.      jump we were going to */
  140.  
  141.   gap = value - (dot - shrink);
  142.   
  143.  
  144.   if (-120 < (long)gap && (long)gap < 120 )
  145.   { 
  146.  
  147.     /* Change the reloc type from 16bit, possible 8 to 8bit
  148.        possible 16 */
  149.     r->howto = r->howto + 1;      
  150.     /* The place to relc moves back by one */
  151.     r->address -=1;
  152.       
  153.     /* This will be two bytes smaller in the long run */
  154.     shrink +=2 ;
  155.     perform_slip(symbols, 2, input_section, r->address-shrink +1);
  156.  
  157.       
  158.   }      
  159.   return shrink;      
  160. }
  161.  
  162. boolean 
  163. DEFUN(bfd_coff_reloc16_relax_section,(abfd, i, symbols),
  164.       bfd *abfd AND
  165.       asection *i AND
  166.       asymbol **symbols)
  167. {
  168.   
  169.   /* Get enough memory to hold the stuff */
  170.   bfd *input_bfd = i->owner;
  171.   asection *input_section = i;
  172.   int shrink = 0 ;
  173.   boolean new = false;
  174.   
  175.   bfd_size_type reloc_size = bfd_get_reloc_upper_bound(input_bfd,
  176.                                input_section);
  177.   arelent **reloc_vector = (arelent **)bfd_xmalloc(reloc_size);
  178.  
  179.   /* Get the relocs and think about them */
  180.   if (bfd_canonicalize_reloc(input_bfd, 
  181.                  input_section,
  182.                  reloc_vector,
  183.                  symbols))
  184.   {
  185.     arelent **parent;
  186.     for (parent = reloc_vector; *parent; parent++) 
  187.     {
  188.       arelent *r = *parent;
  189.       switch (r->howto->type) {
  190.     case R_MOVB2:
  191.     case R_JMP2:
  192.       
  193.       shrink+=2;
  194.       break;
  195.       
  196.     case R_MOVB1:
  197.       shrink = movb1(input_section, symbols, r, shrink);
  198.       new = true;
  199.       
  200.       break;
  201.     case R_JMP1:
  202.       shrink = jmp1(input_section, symbols, r, shrink);
  203.       new = true;
  204.       
  205.       break;
  206.     }
  207.     }
  208.  
  209.   }
  210.   input_section->_cooked_size -= shrink;  
  211.   free((char *)reloc_vector);
  212.   return new;
  213. }
  214.  
  215. bfd_byte *
  216. DEFUN(bfd_coff_reloc16_get_relocated_section_contents,(in_abfd,
  217.                                seclet,
  218.                                data,
  219.                                relocateable),
  220.       bfd *in_abfd AND
  221.       bfd_seclet_type *seclet AND
  222.       bfd_byte *data AND
  223.       boolean relocateable)
  224. {
  225.   /* Get enough memory to hold the stuff */
  226.   bfd *input_bfd = seclet->u.indirect.section->owner;
  227.   asection *input_section = seclet->u.indirect.section;
  228.   bfd_size_type reloc_size = bfd_get_reloc_upper_bound(input_bfd,
  229.                                input_section);
  230.   arelent **reloc_vector = (arelent **)bfd_xmalloc(reloc_size);
  231.   
  232.   /* If producing relocateable output, don't bother to relax.  */
  233.   if (relocateable)
  234.     return bfd_generic_get_relocated_section_contents (in_abfd, seclet,
  235.                                data, relocateable);
  236.  
  237.   /* read in the section */
  238.   bfd_get_section_contents(input_bfd,
  239.                input_section,
  240.                data,
  241.                0,
  242.                input_section->_raw_size);
  243.   
  244.   
  245.   if (bfd_canonicalize_reloc(input_bfd, 
  246.                  input_section,
  247.                  reloc_vector,
  248.                  seclet->u.indirect.symbols) )
  249.   {
  250.     arelent **parent = reloc_vector;
  251.     arelent *reloc ;
  252.     
  253.  
  254.  
  255.     unsigned int dst_address = 0;
  256.     unsigned int src_address = 0;
  257.     unsigned int run;
  258.     unsigned int idx;
  259.     
  260.     /* Find how long a run we can do */
  261.     while (dst_address < seclet->size) 
  262.     {
  263.       
  264.       reloc = *parent;
  265.       if (reloc) 
  266.       {
  267.     /* Note that the relaxing didn't tie up the addresses in the
  268.        relocation, so we use the original address to work out the
  269.        run of non-relocated data */
  270.     run = reloc->address - src_address;
  271.     parent++;
  272.     
  273.       }
  274.       else 
  275.       {
  276.     run = seclet->size - dst_address;
  277.       }
  278.       /* Copy the bytes */
  279.       for (idx = 0; idx < run; idx++)
  280.       {
  281.     data[dst_address++] = data[src_address++];
  282.       }
  283.     
  284.       /* Now do the relocation */
  285.     
  286.       if (reloc) 
  287.       {
  288.     switch (reloc->howto->type) 
  289.     {
  290.     case R_JMP2:
  291.       /* Speciial relaxed type */
  292.     {
  293.       bfd_vma dot = seclet->offset + dst_address + seclet->u.indirect.section->output_section->vma;      
  294.       int   gap = bfd_coff_reloc16_get_value(reloc,seclet)-dot-1;
  295.       if ((gap & ~0xff  ) != 0 &&((gap & 0xff00)!= 0xff00)) abort();
  296.  
  297.       bfd_put_8(in_abfd,gap,   data+dst_address);
  298.  
  299.       switch (data[dst_address-1]) 
  300.       {
  301.         
  302.       case 0x5e:
  303.         /* jsr -> bsr */
  304.         bfd_put_8(in_abfd, 0x55, data+dst_address-1);
  305.         break;
  306.       case 0x5a:     
  307.         /* jmp ->bra */
  308.         bfd_put_8(in_abfd, 0x40, data+dst_address-1);
  309.         break;
  310.       
  311.       default:
  312.         abort();
  313.       
  314.       }
  315.     
  316.     
  317.  
  318.          
  319.       dst_address++;
  320.       src_address+=3;
  321.   
  322.       break;
  323.     }
  324.  
  325.          
  326.     case R_MOVB2:
  327.       /* Special relaxed type, there will be a gap between where we
  328.          get stuff from and where we put stuff to now 
  329.          
  330.          for a mov.b @aa:16 -> mov.b @aa:8
  331.          opcode 0x6a 0x0y offset
  332.          ->     0x2y off
  333.          */
  334.       if (data[dst_address-1] != 0x6a)
  335.        abort();
  336.       switch (data[src_address] & 0xf0) 
  337.       {
  338.       case 0x00:
  339.         /* Src is memory */
  340.         data[dst_address-1] = (data[src_address] & 0xf) | 0x20;
  341.         break;
  342.       case 0x80:
  343.         /* Src is reg */
  344.         data[dst_address-1] = (data[src_address] & 0xf) | 0x30;
  345.         break;
  346.       default:
  347.         abort();
  348.       }
  349.     
  350.       /* the offset must fit ! after all, what was all the relaxing
  351.          about ? */
  352.  
  353.       bfd_put_8(in_abfd, bfd_coff_reloc16_get_value(reloc, seclet),
  354.             data + dst_address);
  355.  
  356.       /* Note the magic - src goes up by two bytes, but dst by only
  357.          one */
  358.       dst_address+=1;
  359.       src_address+=3;
  360.     
  361.       break;
  362.       /* PCrel 8 bits */
  363.     case R_PCRBYTE:      
  364.     {
  365.       bfd_vma dot = seclet->offset + dst_address + seclet->u.indirect.section->output_section->vma;      
  366.       int   gap = bfd_coff_reloc16_get_value(reloc,seclet)-dot;
  367.       if (gap > 127 || gap < -128) 
  368.       {
  369.         bfd_error_vector.reloc_value_truncated(reloc, seclet);
  370.       }
  371.       
  372.       bfd_put_8(in_abfd,gap,   data+dst_address);
  373.       dst_address++;
  374.       src_address++;
  375.   
  376.       break;
  377.     }
  378.  
  379.     case R_RELBYTE:
  380.     {
  381.       unsigned  int gap =bfd_coff_reloc16_get_value(reloc,seclet);
  382.       if (gap > 0xff && gap < ~0xff)
  383.       {
  384.         bfd_error_vector.reloc_value_truncated(reloc, seclet);
  385.       }
  386.       
  387.       bfd_put_8(in_abfd, gap, data+dst_address);
  388.       dst_address+=1;
  389.       src_address+=1;
  390.  
  391.  
  392.     }
  393.       break; 
  394.     case R_JMP1:
  395.       /* A relword which would have like to have been a pcrel */
  396.     case R_MOVB1:    
  397.       /* A relword which would like to have been modified but
  398.          didn't make it */
  399.     case R_RELWORD:
  400.       bfd_put_16(in_abfd, bfd_coff_reloc16_get_value(reloc,seclet),
  401.              data+dst_address);
  402.       dst_address+=2;
  403.       src_address+=2;
  404.       break;
  405.         default:
  406.       bfd_coff_reloc16_extra_cases (in_abfd, seclet, reloc, data,
  407.                     &src_address, &dst_address);
  408.       break;
  409.     }
  410.       }    
  411.     }
  412.   }
  413.   free((char *)reloc_vector);
  414.   return data;
  415.   
  416. }
  417.  
  418.